<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>珠峰架构师成长计划</title>
    <link rel="stylesheet" type="text/css" href="../static/css/main.css">
</head>
<body>
<div class="nav">
    <div class="logo">
        
            珠峰架构师成长计划
        
    </div>
<ul><li><a href="../index.html">0.Async</a></li><li><a href="../html/0.editor.html">0.editor</a></li><li><a href="../html/0.module.html">0.module</a></li><li><a href="../html/1.ES2015.html">1.ES2015</a></li><li><a href="../html/2.Promise.html">2.Promise</a></li><li><a href="../html/3.Node.html">3.Node</a></li><li><a href="../html/4.NodeInstall.html">4.NodeInstall</a></li><li><a href="../html/5.REPL.html">5.REPL</a></li><li><a href="../html/6.NodeCore.html">6.NodeCore</a></li><li><a href="../html/7.module&NPM.html">7.module&NPM</a></li><li><a href="../html/8.Encoding.html">8.Encoding</a></li><li><a href="../html/9.Buffer.html">9.Buffer</a></li><li><a href="../html/10.fs.html">10.fs</a></li><li><a href="../html/11.Stream-1.html">11.Stream-1</a></li><li><a href="../html/11.Stream-2.html">11.Stream-2</a></li><li><a href="../html/11.Stream-3.html">11.Stream-3</a></li><li><a href="../html/11.Stream-4.html">11.Stream-4</a></li><li><a href="../html/12-Network-2.html">12-Network-2</a></li><li><a href="../html/12.NetWork-3.html">12.NetWork-3</a></li><li><a href="../html/12.Network-1.html">12.Network-1</a></li><li><a href="../html/13.tcp.html">13.tcp</a></li><li><a href="../html/14.http-1.html">14.http-1</a></li><li><a href="../html/14.http-2.html">14.http-2</a></li><li><a href="../html/15.compress.html">15.compress</a></li><li><a href="../html/16.crypto.html">16.crypto</a></li><li><a href="../html/17.process.html">17.process</a></li><li><a href="../html/18.yargs.html">18.yargs</a></li><li><a href="../html/19.cache.html">19.cache</a></li><li><a href="../html/20.action.html">20.action</a></li><li><a href="../html/21.https.html">21.https</a></li><li><a href="../html/22.cookie.html">22.cookie</a></li><li><a href="../html/23.session.html">23.session</a></li><li><a href="../html/24.express-1.html">24.express-1</a></li><li><a href="../html/24.express-2.html">24.express-2</a></li><li><a href="../html/24.express-3.html">24.express-3</a></li><li><a href="../html/24.express-4.html">24.express-4</a></li><li><a href="../html/25.koa-1.html">25.koa-1</a></li><li><a href="../html/26.webpack-1-basic.html">26.webpack-1-basic</a></li><li><a href="../html/26.webpack-2-optimize.html">26.webpack-2-optimize</a></li><li><a href="../html/26.webpack-3.tapable.html">26.webpack-3.tapable</a></li><li><a href="../html/26.webpack-4-AST.html">26.webpack-4-AST</a></li><li><a href="../html/26.webpack-5-source.html">26.webpack-5-source</a></li><li><a href="../html/26.webpack-6-loader.html">26.webpack-6-loader</a></li><li><a href="../html/26.webpack-7-plugin.html">26.webpack-7-plugin</a></li><li class="active"><a href="../html/26.webpack-8-hand.html">26.webpack-8-hand</a></li><li><a href="../html/27.react-1.html">27.react-1</a></li><li><a href="../html/27.react-2.html">27.react-2</a></li><li><a href="../html/27.react-3.html">27.react-3</a></li><li><a href="../html/27.react-4-immutable.html">27.react-4-immutable</a></li><li><a href="../html/27.react-5-react-dom-diff.html">27.react-5-react-dom-diff</a></li><li><a href="../html/27.react-6.html">27.react-6</a></li><li><a href="../html/28.react-mobx.html">28.react-mobx</a></li><li><a href="../html/28.redux-0.html">28.redux-0</a></li><li><a href="../html/28.redux-1.html">28.redux-1</a></li><li><a href="../html/28.redux-2-中间件.html">28.redux-2-中间件</a></li><li><a href="../html/28.redux-3-saga.html">28.redux-3-saga</a></li><li><a href="../html/28.redux-jwt-back.html">28.redux-jwt-back</a></li><li><a href="../html/28.redux-jwt-front.html">28.redux-jwt-front</a></li><li><a href="../html/29.mongodb-1.html">29.mongodb-1</a></li><li><a href="../html/29.mongodb-2.html">29.mongodb-2</a></li><li><a href="../html/29.mongodb-3.html">29.mongodb-3</a></li><li><a href="../html/29.mongodb-4.html">29.mongodb-4</a></li><li><a href="../html/29.mongodb-5.html">29.mongodb-5</a></li><li><a href="../html/29.mongodb-6.html">29.mongodb-6</a></li><li><a href="../html/30.cms-1-mysql.html">30.cms-1-mysql</a></li><li><a href="../html/30.cms-2-mysql.html">30.cms-2-mysql</a></li><li><a href="../html/30.cms-3-mysql.html">30.cms-3-mysql</a></li><li><a href="../html/30.cms-4-egg.html">30.cms-4-egg</a></li><li><a href="../html/30.cms-5-api.html">30.cms-5-api</a></li><li><a href="../html/30.cms-6-roadhog.html">30.cms-6-roadhog</a></li><li><a href="../html/30.cms-7-umi.html">30.cms-7-umi</a></li><li><a href="../html/30.cms-8-dva.html">30.cms-8-dva</a></li><li><a href="../html/30.cms-9-dva.html">30.cms-9-dva</a></li><li><a href="../html/30.cms-10-front.html">30.cms-10-front</a></li><li><a href="../html/30.cms-11-deploy.html">30.cms-11-deploy</a></li><li><a href="../html/33.redis.html">33.redis</a></li><li><a href="../html/34.unittest.html">34.unittest</a></li><li><a href="../html/35.jwt.html">35.jwt</a></li><li><a href="../html/36.websocket-1.html">36.websocket-1</a></li><li><a href="../html/36.websocket-2.html">36.websocket-2</a></li><li><a href="../html/38.chat-api-1.html">38.chat-api-1</a></li><li><a href="../html/38.chat-api-2.html">38.chat-api-2</a></li><li><a href="../html/38.chat-3.html">38.chat-3</a></li><li><a href="../html/38.chat-api-3.html">38.chat-api-3</a></li><li><a href="../html/38.chat.html">38.chat</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/38.chat2.html">38.chat2</a></li><li><a href="../html/39.crawl-0.html">39.crawl-0</a></li><li><a href="../html/39.crawl-1.html">39.crawl-1</a></li><li><a href="../html/39.crawl-2.html">39.crawl-2</a></li><li><a href="../html/40.deploy.html">40.deploy</a></li><li><a href="../html/41.safe.html">41.safe</a></li><li><a href="../html/42.test.html">42.test</a></li><li><a href="../html/43.nginx.html">43.nginx</a></li><li><a href="../html/44.enzyme.html">44.enzyme</a></li><li><a href="../html/45.docker.html">45.docker</a></li><li><a href="../html/46.elastic.html">46.elastic</a></li><li><a href="../html/47.oauth.html">47.oauth</a></li><li><a href="../html/48.wxpay.html">48.wxpay</a></li><li><a href="../html/49.nunjucks.html">49.nunjucks</a></li><li><a href="../html/50.ketang.html">50.ketang</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/51.typescript.html">51.typescript</a></li><li><a href="../html/52.UML.html">52.UML</a></li><li><a href="../html/53.design.html">53.design</a></li><li><a href="../html/index.html">index</a></li><li><a href="../html/54.linux.html">54.linux</a></li><li><a href="../html/55.yaml.html">55.yaml</a></li><li><a href="../html/50.ketang2.html">50.ketang2</a></li><li><a href="../html/56.ts.html">56.ts</a></li><li><a href="../html/57.ts_react.html">57.ts_react</a></li><li><a href="../html/58.react-ssr.html">58.react-ssr</a></li><li><a href="../html/59.react-ssr.html">59.react-ssr</a></li></ul></div>


<div class="warpper">

    <div class="page-toc">
        <ul><li><a href="#t01. Webpack流程概括">1. Webpack流程概括</a></li><li><a href="#t12. 钩子">2. 钩子</a></li><li><a href="#t23. 编写示例项目">3. 编写示例项目</a><ul><li><a href="#t33.1 安装依赖模块">3.1 安装依赖模块</a></li><li><a href="#t43.2  编写webpack配置文件">3.2  编写webpack配置文件</a></li><li><a href="#t53.3 源文件">3.3 源文件</a><ul><li><a href="#t63.3.1 src/index.js">3.3.1 src/index.js</a></li><li><a href="#t73.3.2 src/a1.js">3.3.2 src/a1.js</a></li><li><a href="#t83.3.3 src/base/a2.js">3.3.3 src/base/a2.js</a></li><li><a href="#t93.3.4 产出bundle.js">3.3.4 产出bundle.js</a></li></ul></li></ul></li><li><a href="#t104. 编写webpack">4. 编写webpack</a><ul><li><a href="#t114.1  安装依赖包">4.1  安装依赖包</a></li><li><a href="#t124.2  创建项目">4.2  创建项目</a></li><li><a href="#t134.3  创建可执行文件">4.3  创建可执行文件</a></li><li><a href="#t144.4 创建Compiler对象">4.4 创建Compiler对象</a></li><li><a href="#t154.5 入口模板">4.5 入口模板</a></li><li><a href="#t164.6 产出文件">4.6 产出文件</a></li></ul></li><li><a href="#t175. 支持loader">5. 支持loader</a><ul><li><a href="#t185.1 Compiler">5.1 Compiler</a></li><li><a href="#t195.2 less-loader">5.2 less-loader</a></li><li><a href="#t205.3 style-loader">5.3 style-loader</a></li><li><a href="#t215.4 index.js">5.4 index.js</a></li></ul></li><li><a href="#t226.  支持插件">6.  支持插件</a><ul><li><a href="#t236.1 webpack.config.js">6.1 webpack.config.js</a></li><li><a href="#t246.2 Compiler">6.2 Compiler</a></li></ul></li><li><a href="#t257.支持懒加载">7.支持懒加载</a><ul><li><a href="#t267.1 Compiler">7.1 Compiler</a></li><li><a href="#t277.2 chunk.ejs">7.2 chunk.ejs</a></li><li><a href="#t287.3 index.js">7.3 index.js</a></li><li><a href="#t297.4 video.js">7.4 video.js</a></li></ul></li><li><a href="#t30参考资料">参考资料</a></li></ul>
    </div>
    
    <div class="content markdown-body">
        <h2 id="t01. Webpack&#x6D41;&#x7A0B;&#x6982;&#x62EC;">1. Webpack&#x6D41;&#x7A0B;&#x6982;&#x62EC; <a href="#t01. Webpack&#x6D41;&#x7A0B;&#x6982;&#x62EC;"> # </a></h2>
<ul>
<li>&#x521D;&#x59CB;&#x5316;&#x53C2;&#x6570;&#xFF1A;&#x4ECE;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x548C; Shell &#x8BED;&#x53E5;&#x4E2D;&#x8BFB;&#x53D6;&#x4E0E;&#x5408;&#x5E76;&#x53C2;&#x6570;&#xFF0C;&#x5F97;&#x51FA;&#x6700;&#x7EC8;&#x7684;&#x53C2;&#x6570;&#xFF1B;
&#x5F00;&#x59CB;&#x7F16;&#x8BD1;&#xFF1A;&#x7528;&#x4E0A;&#x4E00;&#x6B65;&#x5F97;&#x5230;&#x7684;&#x53C2;&#x6570;&#x521D;&#x59CB;&#x5316; Compiler &#x5BF9;&#x8C61;&#xFF0C;&#x52A0;&#x8F7D;&#x6240;&#x6709;&#x914D;&#x7F6E;&#x7684;&#x63D2;&#x4EF6;&#xFF0C;&#x6267;&#x884C;&#x5BF9;&#x8C61;&#x7684; run &#x65B9;&#x6CD5;&#x5F00;&#x59CB;&#x6267;&#x884C;&#x7F16;&#x8BD1;&#xFF1B;</li>
<li>&#x786E;&#x5B9A;&#x5165;&#x53E3;&#xFF1A;&#x6839;&#x636E;&#x914D;&#x7F6E;&#x4E2D;&#x7684; entry &#x627E;&#x51FA;&#x6240;&#x6709;&#x7684;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#xFF1B;</li>
<li>&#x7F16;&#x8BD1;&#x6A21;&#x5757;&#xFF1A;&#x4ECE;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#x51FA;&#x53D1;&#xFF0C;&#x8C03;&#x7528;&#x6240;&#x6709;&#x914D;&#x7F6E;&#x7684; Loader &#x5BF9;&#x6A21;&#x5757;&#x8FDB;&#x884C;&#x7FFB;&#x8BD1;&#xFF0C;&#x518D;&#x627E;&#x51FA;&#x8BE5;&#x6A21;&#x5757;&#x4F9D;&#x8D56;&#x7684;&#x6A21;&#x5757;&#xFF0C;&#x518D;&#x9012;&#x5F52;&#x672C;&#x6B65;&#x9AA4;&#x76F4;&#x5230;&#x6240;&#x6709;&#x5165;&#x53E3;&#x4F9D;&#x8D56;&#x7684;&#x6587;&#x4EF6;&#x90FD;&#x7ECF;&#x8FC7;&#x4E86;&#x672C;&#x6B65;&#x9AA4;&#x7684;&#x5904;&#x7406;&#xFF1B;</li>
<li>&#x5B8C;&#x6210;&#x6A21;&#x5757;&#x7F16;&#x8BD1;&#xFF1A;&#x5728;&#x7ECF;&#x8FC7;&#x7B2C;4&#x6B65;&#x4F7F;&#x7528; Loader &#x7FFB;&#x8BD1;&#x5B8C;&#x6240;&#x6709;&#x6A21;&#x5757;&#x540E;&#xFF0C;&#x5F97;&#x5230;&#x4E86;&#x6BCF;&#x4E2A;&#x6A21;&#x5757;&#x88AB;&#x7FFB;&#x8BD1;&#x540E;&#x7684;&#x6700;&#x7EC8;&#x5185;&#x5BB9;&#x4EE5;&#x53CA;&#x5B83;&#x4EEC;&#x4E4B;&#x95F4;&#x7684;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#xFF1B;</li>
<li>&#x8F93;&#x51FA;&#x8D44;&#x6E90;&#xFF1A;&#x6839;&#x636E;&#x5165;&#x53E3;&#x548C;&#x6A21;&#x5757;&#x4E4B;&#x95F4;&#x7684;&#x4F9D;&#x8D56;&#x5173;&#x7CFB;&#xFF0C;&#x7EC4;&#x88C5;&#x6210;&#x4E00;&#x4E2A;&#x4E2A;&#x5305;&#x542B;&#x591A;&#x4E2A;&#x6A21;&#x5757;&#x7684; Chunk&#xFF0C;&#x518D;&#x628A;&#x6BCF;&#x4E2A; Chunk &#x8F6C;&#x6362;&#x6210;&#x4E00;&#x4E2A;&#x5355;&#x72EC;&#x7684;&#x6587;&#x4EF6;&#x52A0;&#x5165;&#x5230;&#x8F93;&#x51FA;&#x5217;&#x8868;&#xFF0C;&#x8FD9;&#x6B65;&#x662F;&#x53EF;&#x4EE5;&#x4FEE;&#x6539;&#x8F93;&#x51FA;&#x5185;&#x5BB9;&#x7684;&#x6700;&#x540E;&#x673A;&#x4F1A;&#xFF1B;</li>
<li>&#x8F93;&#x51FA;&#x5B8C;&#x6210;&#xFF1A;&#x5728;&#x786E;&#x5B9A;&#x597D;&#x8F93;&#x51FA;&#x5185;&#x5BB9;&#x540E;&#xFF0C;&#x6839;&#x636E;&#x914D;&#x7F6E;&#x786E;&#x5B9A;&#x8F93;&#x51FA;&#x7684;&#x8DEF;&#x5F84;&#x548C;&#x6587;&#x4EF6;&#x540D;&#xFF0C;&#x628A;&#x6587;&#x4EF6;&#x5185;&#x5BB9;&#x5199;&#x5165;&#x5230;&#x6587;&#x4EF6;&#x7CFB;&#x7EDF;&#x3002;<blockquote>
<p>&#x5728;&#x4EE5;&#x4E0A;&#x8FC7;&#x7A0B;&#x4E2D;&#xFF0C;Webpack &#x4F1A;&#x5728;&#x7279;&#x5B9A;&#x7684;&#x65F6;&#x95F4;&#x70B9;&#x5E7F;&#x64AD;&#x51FA;&#x7279;&#x5B9A;&#x7684;&#x4E8B;&#x4EF6;&#xFF0C;&#x63D2;&#x4EF6;&#x5728;&#x76D1;&#x542C;&#x5230;&#x611F;&#x5174;&#x8DA3;&#x7684;&#x4E8B;&#x4EF6;&#x540E;&#x4F1A;&#x6267;&#x884C;&#x7279;&#x5B9A;&#x7684;&#x903B;&#x8F91;&#xFF0C;&#x5E76;&#x4E14;&#x63D2;&#x4EF6;&#x53EF;&#x4EE5;&#x8C03;&#x7528; Webpack &#x63D0;&#x4F9B;&#x7684; API &#x6539;&#x53D8; Webpack &#x7684;&#x8FD0;&#x884C;&#x7ED3;&#x679C;&#x3002;</p>
</blockquote>
</li>
</ul>
<h2 id="t12. &#x94A9;&#x5B50;">2. &#x94A9;&#x5B50; <a href="#t12. &#x94A9;&#x5B50;"> # </a></h2>
<ul>
<li>entryOption &#x8BFB;&#x53D6;&#x914D;&#x7F6E;&#x6587;&#x4EF6;</li>
<li>afterPlugins &#x52A0;&#x8F7D;&#x6240;&#x6709;&#x7684;&#x63D2;&#x4EF6;</li>
<li>run &#x5F00;&#x59CB;&#x6267;&#x884C;&#x7F16;&#x8BD1;&#x6D41;&#x7A0B;</li>
<li>compile &#x5F00;&#x59CB;&#x7F16;&#x8BD1;</li>
<li>afterCompile &#x7F16;&#x8BD1;&#x5B8C;&#x6210;</li>
<li>emit &#x5199;&#x5165;&#x6587;&#x4EF6;</li>
<li>done  &#x5B8C;&#x6210;&#x6574;&#x4F53;&#x6D41;&#x7A0B;</li>
</ul>
<h2 id="t23. &#x7F16;&#x5199;&#x793A;&#x4F8B;&#x9879;&#x76EE;">3. &#x7F16;&#x5199;&#x793A;&#x4F8B;&#x9879;&#x76EE; <a href="#t23. &#x7F16;&#x5199;&#x793A;&#x4F8B;&#x9879;&#x76EE;"> # </a></h2>
<h3 id="t33.1 &#x5B89;&#x88C5;&#x4F9D;&#x8D56;&#x6A21;&#x5757;">3.1 &#x5B89;&#x88C5;&#x4F9D;&#x8D56;&#x6A21;&#x5757; <a href="#t33.1 &#x5B89;&#x88C5;&#x4F9D;&#x8D56;&#x6A21;&#x5757;"> # </a></h3>
<pre><code class="lang-js">$ npm init -y
$ yarn add webpack webpack-cli html-webpack-plugin
</code></pre>
<h3 id="t43.2  &#x7F16;&#x5199;webpack&#x914D;&#x7F6E;&#x6587;&#x4EF6;">3.2  &#x7F16;&#x5199;webpack&#x914D;&#x7F6E;&#x6587;&#x4EF6; <a href="#t43.2  &#x7F16;&#x5199;webpack&#x914D;&#x7F6E;&#x6587;&#x4EF6;"> # </a></h3>
<p>webpack.config.js</p>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-built_in">module</span>.exports = {
    <span class="hljs-attr">mode</span>: <span class="hljs-string">&apos;development&apos;</span>,
    <span class="hljs-attr">entry</span>: <span class="hljs-string">&apos;./src/index.js&apos;</span>,
    <span class="hljs-attr">output</span>: {
        <span class="hljs-attr">path</span>: path.resolve(__dirname, <span class="hljs-string">&apos;dist&apos;</span>),
        <span class="hljs-attr">filename</span>: <span class="hljs-string">&apos;bundle.js&apos;</span>
    },
    <span class="hljs-attr">module</span>: {},
    <span class="hljs-attr">plugins</span>: []
}
</code></pre>
<h3 id="t53.3 &#x6E90;&#x6587;&#x4EF6;">3.3 &#x6E90;&#x6587;&#x4EF6; <a href="#t53.3 &#x6E90;&#x6587;&#x4EF6;"> # </a></h3>
<h4 id="t63.3.1 src/index.js">3.3.1 src/index.js <a href="#t63.3.1 src/index.js"> # </a></h4>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> a1=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./a1&apos;</span>);
<span class="hljs-built_in">console</span>.log(a1);
</code></pre>
<h4 id="t73.3.2 src/a1.js">3.3.2 src/a1.js <a href="#t73.3.2 src/a1.js"> # </a></h4>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> a2=<span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./base/a2&apos;</span>);
<span class="hljs-built_in">module</span>.exports=<span class="hljs-string">&apos;a1&apos;</span>+a2;
</code></pre>
<h4 id="t83.3.3 src/base/a2.js">3.3.3 src/base/a2.js <a href="#t83.3.3 src/base/a2.js"> # </a></h4>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports=<span class="hljs-string">&apos;a2&apos;</span>;
</code></pre>
<h4 id="t93.3.4 &#x4EA7;&#x51FA;bundle.js">3.3.4 &#x4EA7;&#x51FA;bundle.js <a href="#t93.3.4 &#x4EA7;&#x51FA;bundle.js"> # </a></h4>
<pre><code class="lang-js">(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">modules</span>) </span>{
  <span class="hljs-keyword">var</span> installedModules = {};
  <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__webpack_require__</span>(<span class="hljs-params">moduleId</span>) </span>{
    <span class="hljs-keyword">if</span> (installedModules[moduleId]) {
      <span class="hljs-keyword">return</span> installedModules[moduleId].exports;
    }
    <span class="hljs-keyword">var</span> <span class="hljs-built_in">module</span> = installedModules[moduleId] = {
      <span class="hljs-attr">i</span>: moduleId,
      <span class="hljs-attr">l</span>: <span class="hljs-literal">false</span>,
      <span class="hljs-attr">exports</span>: {}
    };

    modules[moduleId].call(<span class="hljs-built_in">module</span>.exports, <span class="hljs-built_in">module</span>, <span class="hljs-built_in">module</span>.exports, __webpack_require__);
    <span class="hljs-built_in">module</span>.l = <span class="hljs-literal">true</span>;
    <span class="hljs-keyword">return</span> <span class="hljs-built_in">module</span>.exports;
  }
  <span class="hljs-keyword">return</span> __webpack_require__(__webpack_require__.s = <span class="hljs-string">&quot;./src/index.js&quot;</span>);
})
  ({

    <span class="hljs-string">&quot;./src/a1.js&quot;</span>:
      (<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">module, exports, __webpack_require__</span>) </span>{
        <span class="hljs-built_in">eval</span>(<span class="hljs-string">&quot;let a2 = __webpack_require__( \&quot;./src/base/a2.js\&quot;);\r\nmodule.exports = &apos;a1&apos; + a2;&quot;</span>);
      }),
    <span class="hljs-string">&quot;./src/base/a2.js&quot;</span>:
      (<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">module, exports</span>) </span>{
        <span class="hljs-built_in">eval</span>(<span class="hljs-string">&quot;module.exports = &apos;a2&apos;;&quot;</span>);
      }),

    <span class="hljs-string">&quot;./src/index.js&quot;</span>:
      (<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">module, exports, __webpack_require__</span>) </span>{
        <span class="hljs-built_in">eval</span>(<span class="hljs-string">&quot;let a1 = __webpack_require__(\&quot;./src/a1.js\&quot;);\r\nconsole.log(a1);&quot;</span>);
      })
  });
</code></pre>
<h2 id="t104. &#x7F16;&#x5199;webpack">4. &#x7F16;&#x5199;webpack <a href="#t104. &#x7F16;&#x5199;webpack"> # </a></h2>
<h3 id="t114.1  &#x5B89;&#x88C5;&#x4F9D;&#x8D56;&#x5305;">4.1  &#x5B89;&#x88C5;&#x4F9D;&#x8D56;&#x5305; <a href="#t114.1  &#x5B89;&#x88C5;&#x4F9D;&#x8D56;&#x5305;"> # </a></h3>
<pre><code>$ yarn add babel-types babel-generator babel-traverse
</code></pre><h3 id="t124.2  &#x521B;&#x5EFA;&#x9879;&#x76EE;">4.2  &#x521B;&#x5EFA;&#x9879;&#x76EE; <a href="#t124.2  &#x521B;&#x5EFA;&#x9879;&#x76EE;"> # </a></h3>
<p>package.json</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">&quot;name&quot;</span>: <span class="hljs-string">&quot;webpackhand&quot;</span>,
  <span class="hljs-attr">&quot;version&quot;</span>: <span class="hljs-string">&quot;1.0.0&quot;</span>,
  <span class="hljs-attr">&quot;description&quot;</span>: <span class="hljs-string">&quot;&quot;</span>,
  <span class="hljs-attr">&quot;main&quot;</span>: <span class="hljs-string">&quot;index.js&quot;</span>,
  <span class="hljs-attr">&quot;bin&quot;</span>: {
    <span class="hljs-attr">&quot;webpackhand&quot;</span>: <span class="hljs-string">&quot;./bin/webpackhand.js&quot;</span>
  },
  <span class="hljs-attr">&quot;keywords&quot;</span>: [],
  <span class="hljs-attr">&quot;author&quot;</span>: <span class="hljs-string">&quot;&quot;</span>,
  <span class="hljs-attr">&quot;license&quot;</span>: <span class="hljs-string">&quot;ISC&quot;</span>
}
</code></pre>
<h3 id="t134.3  &#x521B;&#x5EFA;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;">4.3  &#x521B;&#x5EFA;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6; <a href="#t134.3  &#x521B;&#x5EFA;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;"> # </a></h3>
<p>bin\webpackhand.js</p>
<pre><code class="lang-js"><span class="hljs-meta">#! /usr/bin/env node</span>
<span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">const</span> Compiler = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;../lib/Compiler&apos;</span>);
<span class="hljs-comment">//&#x547D;&#x4EE4;&#x7684;&#x5F53;&#x524D;&#x5DE5;&#x4F5C;&#x76EE;&#x5F55;</span>
<span class="hljs-keyword">const</span> root = process.cwd();
<span class="hljs-comment">//&#x5339;&#x914D;&#x914D;&#x7F6E;&#x6587;&#x4EF6;&#x5BF9;&#x8C61;</span>
<span class="hljs-keyword">let</span> options = <span class="hljs-built_in">require</span>(path.resolve(<span class="hljs-string">&apos;webpack.config.js&apos;</span>));
<span class="hljs-keyword">let</span> compiler = <span class="hljs-keyword">new</span> Compiler(options);
compiler.run();
</code></pre>
<h3 id="t144.4 &#x521B;&#x5EFA;Compiler&#x5BF9;&#x8C61;">4.4 &#x521B;&#x5EFA;Compiler&#x5BF9;&#x8C61; <a href="#t144.4 &#x521B;&#x5EFA;Compiler&#x5BF9;&#x8C61;"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">const</span> babylon = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;babylon&apos;</span>);
<span class="hljs-keyword">const</span> t = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;babel-types&apos;</span>);
<span class="hljs-keyword">const</span> generate = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;babel-generator&apos;</span>).default;
<span class="hljs-keyword">const</span> traverse = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;babel-traverse&apos;</span>).default;
<span class="hljs-keyword">const</span> ejs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;ejs&apos;</span>);

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Compiler</span> </span>{
    <span class="hljs-keyword">constructor</span>(options) {
        <span class="hljs-keyword">this</span>.options = options;
    }
    run() {
        <span class="hljs-keyword">let</span> that = <span class="hljs-keyword">this</span>;
        <span class="hljs-keyword">this</span>.root = process.cwd();<span class="hljs-comment">//&#x83B7;&#x53D6;&#x5F53;&#x524D;&#x7684;&#x5DE5;&#x4F5C;&#x76EE;&#x5F55;</span>
        <span class="hljs-keyword">let</span> { entry } = <span class="hljs-keyword">this</span>.options;<span class="hljs-comment">//&#x83B7;&#x53D6;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#x8DEF;&#x5F84;</span>
        <span class="hljs-keyword">this</span>.entryId = <span class="hljs-literal">null</span>;<span class="hljs-comment">//&#x8BB0;&#x5F55;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#x7684;ID</span>
        <span class="hljs-keyword">this</span>.modules = {};<span class="hljs-comment">//&#x8BB0;&#x5F55;&#x6A21;&#x5757;ID&#x548C;&#x5185;&#x5BB9;&#x7684;&#x5BF9;&#x5E94;&#x5173;&#x7CFB;&#xFF0C;&#x6240;&#x6709;&#x7684;ID&#x90FD;&#x662F;&#x76F8;&#x5BF9;&#x4E8E;&#x6839;&#x76EE;&#x5F55;&#x7684;</span>
        <span class="hljs-keyword">this</span>.buildModule(path.resolve(<span class="hljs-keyword">this</span>.root, entry), <span class="hljs-literal">true</span>);<span class="hljs-comment">//&#x4ECE;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#x5F00;&#x59CB;&#x7F16;&#x8BD1;</span>
        <span class="hljs-built_in">console</span>.log(<span class="hljs-keyword">this</span>.modules);
        <span class="hljs-keyword">this</span>.emitFile();
    }
    emitFile() {
        <span class="hljs-keyword">let</span> mainTemplate = fs.readFileSync(path.join(__dirname, <span class="hljs-string">&apos;main.ejs&apos;</span>), <span class="hljs-string">&apos;utf8&apos;</span>);
        <span class="hljs-keyword">let</span> { modules, entryId } = <span class="hljs-keyword">this</span>;
        <span class="hljs-keyword">let</span> main = ejs.compile(mainTemplate)({ entryId, modules });
        <span class="hljs-keyword">let</span> { <span class="hljs-attr">output</span>: { <span class="hljs-attr">path</span>: dist, filename } } = <span class="hljs-keyword">this</span>.options;
        fs.writeFileSync(path.join(dist, filename), main);
    }
    getSource(modulePath) {
        <span class="hljs-keyword">return</span> fs.readFileSync(modulePath, <span class="hljs-string">&apos;utf8&apos;</span>);
    }
    <span class="hljs-comment">//&#x89E3;&#x6790;&#x6A21;&#x5757;&#x548C;&#x4F9D;&#x8D56;&#x7684;&#x6A21;&#x5757;&#xFF0C;&#x8DEF;&#x5F84;&#x662F;&#x4E00;&#x4E2A;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;</span>
    buildModule(modulePath, isEntry) {
        <span class="hljs-keyword">let</span> source = <span class="hljs-keyword">this</span>.getSource(modulePath);<span class="hljs-comment">//&#x83B7;&#x53D6;&#x6E90;&#x4EE3;&#x7801;</span>
        <span class="hljs-keyword">let</span> moduleId = <span class="hljs-string">&apos;./&apos;</span> + path.relative(<span class="hljs-keyword">this</span>.root, modulePath);<span class="hljs-comment">//&#x751F;&#x6210;&#x76F8;&#x5BF9;&#x4E8E;&#x5DE5;&#x4F5C;&#x6839;&#x76EE;&#x5F55;&#x7684;&#x6A21;&#x5757;ID</span>
        <span class="hljs-keyword">if</span> (isEntry) {<span class="hljs-comment">//&#x5982;&#x679C;&#x662F;&#x5165;&#x53E3;&#x7684;&#x8BDD;&#x628A;ID&#x8D4B;&#x7ED9;&#x5165;&#x53E3;</span>
            <span class="hljs-keyword">this</span>.entryId = moduleId;
        }
        <span class="hljs-comment">//&#x83B7;&#x53D6;AST&#x7684;&#x7F16;&#x8BD1;&#x7ED3;&#x679C; &#x4F9D;&#x8D56;&#x7684;&#x6A21;&#x5757; &#x8F6C;&#x6362;&#x540E;&#x7684;&#x6E90;&#x4EE3;&#x7801;</span>
        <span class="hljs-keyword">let</span> { dependencies, sourcecode } = <span class="hljs-keyword">this</span>.parse(source, path.dirname(moduleId));
        <span class="hljs-keyword">this</span>.modules[moduleId] = sourcecode;
        <span class="hljs-comment">//&#x9012;&#x5F52;&#x89E3;&#x6790;&#x4F9D;&#x8D56;&#x7684;&#x6A21;&#x5757;</span>
        dependencies.forEach(<span class="hljs-function"><span class="hljs-params">dependency</span> =&gt;</span> <span class="hljs-keyword">this</span>.buildModule(path.join(<span class="hljs-keyword">this</span>.root, dependency)));
    }
    <span class="hljs-comment">//&#x89E3;&#x6790;&#x6E90;&#x4EE3;&#x7801;  &#x4F20;&#x5165;&#x7236;&#x8DEF;&#x5F84;</span>
    parse(source, parentPath) {
        <span class="hljs-keyword">let</span> that = <span class="hljs-keyword">this</span>;
        <span class="hljs-keyword">const</span> ast = babylon.parse(source);
        <span class="hljs-keyword">let</span> dependencies = [];
        traverse(ast, {
            CallExpression(p) {
                <span class="hljs-keyword">if</span> (p.node.callee.name == <span class="hljs-string">&apos;require&apos;</span>) {
                    <span class="hljs-keyword">let</span> node = p.node;
                    node.callee.name = <span class="hljs-string">&apos;__webpack_require__&apos;</span>;
                    <span class="hljs-keyword">let</span> modName = node.arguments[<span class="hljs-number">0</span>].value;
                    modName += (modName.lastIndexOf(<span class="hljs-string">&apos;.&apos;</span>) &gt; <span class="hljs-number">0</span> ? <span class="hljs-string">&apos;&apos;</span> : <span class="hljs-string">&apos;.js&apos;</span>);
                    <span class="hljs-keyword">let</span> moduleId = <span class="hljs-string">&apos;./&apos;</span> + path.join(parentPath, modName);
                    dependencies.push(moduleId);
                    node.arguments = [t.stringLiteral(moduleId)];
                }
            }
        });
        <span class="hljs-keyword">let</span> sourcecode = generate(ast).code;
        <span class="hljs-keyword">return</span> { sourcecode, dependencies };
    }
}
<span class="hljs-built_in">module</span>.exports = Compiler;
</code></pre>
<h3 id="t154.5 &#x5165;&#x53E3;&#x6A21;&#x677F;">4.5 &#x5165;&#x53E3;&#x6A21;&#x677F; <a href="#t154.5 &#x5165;&#x53E3;&#x6A21;&#x677F;"> # </a></h3>
<p>main.ejs</p>
<pre><code class="lang-js">(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">modules</span>) </span>{
    <span class="hljs-keyword">var</span> installedModules = {};
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__webpack_require__</span>(<span class="hljs-params">moduleId</span>) </span>{
      <span class="hljs-keyword">if</span> (installedModules[moduleId]) {
        <span class="hljs-keyword">return</span> installedModules[moduleId].exports;
      }
      <span class="hljs-keyword">var</span> <span class="hljs-built_in">module</span> = installedModules[moduleId] = {
        <span class="hljs-attr">i</span>: moduleId,
        <span class="hljs-attr">l</span>: <span class="hljs-literal">false</span>,
        <span class="hljs-attr">exports</span>: {}
      };

      modules[moduleId].call(<span class="hljs-built_in">module</span>.exports, <span class="hljs-built_in">module</span>, <span class="hljs-built_in">module</span>.exports, __webpack_require__);
      <span class="hljs-built_in">module</span>.l = <span class="hljs-literal">true</span>;
      <span class="hljs-keyword">return</span> <span class="hljs-built_in">module</span>.exports;
    }
    <span class="hljs-keyword">return</span> __webpack_require__(__webpack_require__.s = <span class="hljs-string">&quot;&lt;%-entryId%&gt;&quot;</span>);
  })
    ({
        &lt;%
          <span class="hljs-keyword">for</span>(<span class="hljs-keyword">let</span> id <span class="hljs-keyword">in</span> modules){
              <span class="hljs-keyword">let</span> source = modules[id];%&gt;
              <span class="hljs-string">&quot;&lt;%-id%&gt;&quot;</span>:
              (<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">module, exports,__webpack_require__</span>) </span>{
                <span class="hljs-built_in">eval</span>(<span class="hljs-string">`&lt;%-source%&gt;`</span>);
              }),
           &lt;%}
        %&gt;
    });
</code></pre>
<h3 id="t164.6 &#x4EA7;&#x51FA;&#x6587;&#x4EF6;">4.6 &#x4EA7;&#x51FA;&#x6587;&#x4EF6; <a href="#t164.6 &#x4EA7;&#x51FA;&#x6587;&#x4EF6;"> # </a></h3>
<pre><code class="lang-js">(<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">modules</span>) </span>{
    <span class="hljs-keyword">var</span> installedModules = {};
    <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">__webpack_require__</span>(<span class="hljs-params">moduleId</span>) </span>{
      <span class="hljs-keyword">if</span> (installedModules[moduleId]) {
        <span class="hljs-keyword">return</span> installedModules[moduleId].exports;
      }
      <span class="hljs-keyword">var</span> <span class="hljs-built_in">module</span> = installedModules[moduleId] = {
        <span class="hljs-attr">i</span>: moduleId,
        <span class="hljs-attr">l</span>: <span class="hljs-literal">false</span>,
        <span class="hljs-attr">exports</span>: {}
      };

      modules[moduleId].call(<span class="hljs-built_in">module</span>.exports, <span class="hljs-built_in">module</span>, <span class="hljs-built_in">module</span>.exports, __webpack_require__);
      <span class="hljs-built_in">module</span>.l = <span class="hljs-literal">true</span>;
      <span class="hljs-keyword">return</span> <span class="hljs-built_in">module</span>.exports;
    }
    <span class="hljs-keyword">return</span> __webpack_require__(__webpack_require__.s = <span class="hljs-string">&quot;./src\index.js&quot;</span>);
  })
    ({

              <span class="hljs-string">&quot;./src\index.js&quot;</span>:
              (<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">module, exports,__webpack_require__</span>) </span>{
                <span class="hljs-built_in">eval</span>(<span class="hljs-string">`let a1 = __webpack_require__(&quot;./src\\a1.js&quot;);
console.log(a1);`</span>);
              }),

              <span class="hljs-string">&quot;./src\a1.js&quot;</span>:
              (<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">module, exports,__webpack_require__</span>) </span>{
                <span class="hljs-built_in">eval</span>(<span class="hljs-string">`let a2 = __webpack_require__(&quot;./src\\base\\a2.js&quot;);
module.exports = &apos;a1&apos; + a2;`</span>);
              }),

              <span class="hljs-string">&quot;./src\base\a2.js&quot;</span>:
              (<span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">module, exports,__webpack_require__</span>) </span>{
                <span class="hljs-built_in">eval</span>(<span class="hljs-string">`module.exports = &apos;a2&apos;;`</span>);
              }),

    });
</code></pre>
<h2 id="t175. &#x652F;&#x6301;loader">5. &#x652F;&#x6301;loader <a href="#t175. &#x652F;&#x6301;loader"> # </a></h2>
<h3 id="t185.1 Compiler">5.1 Compiler <a href="#t185.1 Compiler"> # </a></h3>
<pre><code class="lang-js"> getSource(modulePath) {
        <span class="hljs-keyword">let</span> that = <span class="hljs-keyword">this</span>;
        <span class="hljs-keyword">let</span> source = fs.readFileSync(modulePath, <span class="hljs-string">&apos;utf8&apos;</span>);
        <span class="hljs-keyword">let</span> { <span class="hljs-attr">module</span>: { rules } } = <span class="hljs-keyword">this</span>.options;
        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">let</span> i = <span class="hljs-number">0</span>; i &lt; rules.length; i++) {
            <span class="hljs-keyword">let</span> rule = rules[i];
            <span class="hljs-keyword">if</span> (rule.test.test(modulePath)) {
                <span class="hljs-keyword">let</span> loaders = rule.use;
                <span class="hljs-keyword">let</span> loaderIndex = loaders.length - <span class="hljs-number">1</span>;
                <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">iterateLoaders</span>(<span class="hljs-params"></span>) </span>{
                    <span class="hljs-keyword">let</span> loaderName = loaders[loaderIndex--];
                    <span class="hljs-keyword">let</span> loader = <span class="hljs-built_in">require</span>(path.resolve(that.root, <span class="hljs-string">&apos;node_modules&apos;</span>, loaderName));
                    source = loader(source);
                    <span class="hljs-keyword">if</span> (loaderIndex &gt;= <span class="hljs-number">0</span>) {
                        iterateLoaders();
                    }
                }
                iterateLoaders();
                <span class="hljs-keyword">break</span>;
            }
        }
        <span class="hljs-keyword">return</span> source;
    }
</code></pre>
<h3 id="t195.2 less-loader">5.2 less-loader <a href="#t195.2 less-loader"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">var</span> less = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;less&apos;</span>);
<span class="hljs-built_in">module</span>.exports = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">source</span>) </span>{
    <span class="hljs-keyword">let</span> css;
    less.render(source, (err, output) =&gt; {
        css = output.css;
    });
    <span class="hljs-keyword">return</span> css.replace(<span class="hljs-regexp">/\n/g</span>, <span class="hljs-string">&apos;\\n&apos;</span>, <span class="hljs-string">&apos;g&apos;</span>);
}
</code></pre>
<h3 id="t205.3 style-loader">5.3 style-loader <a href="#t205.3 style-loader"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = <span class="hljs-function"><span class="hljs-keyword">function</span> (<span class="hljs-params">source</span>) </span>{
    <span class="hljs-keyword">let</span> str = <span class="hljs-string">`
      let style = document.createElement(&apos;style&apos;);
      style.innerHTML = <span class="hljs-subst">${<span class="hljs-built_in">JSON</span>.stringify(source)}</span>;
      document.head.appendChild(style);
    `</span>;
    <span class="hljs-keyword">return</span> str;
}
</code></pre>
<h3 id="t215.4 index.js">5.4 index.js <a href="#t215.4 index.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;./index.less&apos;</span>);
</code></pre>
<h2 id="t226.  &#x652F;&#x6301;&#x63D2;&#x4EF6;">6.  &#x652F;&#x6301;&#x63D2;&#x4EF6; <a href="#t226.  &#x652F;&#x6301;&#x63D2;&#x4EF6;"> # </a></h2>
<h3 id="t236.1 webpack.config.js">6.1 webpack.config.js <a href="#t236.1 webpack.config.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">EntryOptionWebpackPlugin</span> </span>{
    apply(compiler) {
        compiler.hooks.entryOption.tap(<span class="hljs-string">&apos;Plugin&apos;</span>, (option) =&gt; {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;EntryOptionWebpackPlugin&apos;</span>);
        });
    }
}

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AfterPlugins</span> </span>{
    apply(compiler) {
        compiler.hooks.afterPlugins.tap(<span class="hljs-string">&apos;Plugin&apos;</span>, (option) =&gt; {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;AfterPlugins&apos;</span>);
        });
    }
}


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">RunPlugin</span> </span>{
    apply(compiler) {
        compiler.hooks.run.tap(<span class="hljs-string">&apos;Plugin&apos;</span>, (option) =&gt; {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;RunPlugin&apos;</span>);
        });
    }
}


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">CompileWebpackPlugin</span> </span>{
    apply(compiler) {
        compiler.hooks.compile.tap(<span class="hljs-string">&apos;Plugin&apos;</span>, (option) =&gt; {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;CompileWebpackPlugin&apos;</span>);
        });
    }
}


<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">AfterCompileWebpackPlugin</span> </span>{
    apply(compiler) {
        compiler.hooks.afterCompile.tap(<span class="hljs-string">&apos;Plugin&apos;</span>, (option) =&gt; {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;AfterCompileWebpackPlugin&apos;</span>);
        });
    }
}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">EmitWebpackPlugin</span> </span>{
    apply(compiler) {
        compiler.hooks.emit.tap(<span class="hljs-string">&apos;Plugin&apos;</span>, () =&gt; {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;EmitWebpackPlugin&apos;</span>);
        });
    }
}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">DoneWebpackPlugin</span> </span>{
    apply(compiler) {
        compiler.hooks.done.tap(<span class="hljs-string">&apos;Plugin&apos;</span>, (option) =&gt; {
            <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&apos;DoneWebpackPlugin&apos;</span>);
        });
    }
}
<span class="hljs-built_in">module</span>.exports = {
    <span class="hljs-attr">mode</span>: <span class="hljs-string">&apos;development&apos;</span>,
    <span class="hljs-attr">entry</span>: <span class="hljs-string">&apos;./src/index.js&apos;</span>,
    <span class="hljs-attr">output</span>: {
        <span class="hljs-attr">path</span>: path.resolve(__dirname, <span class="hljs-string">&apos;dist&apos;</span>),
        <span class="hljs-attr">filename</span>: <span class="hljs-string">&apos;bundle.js&apos;</span>
    },
    <span class="hljs-attr">module</span>: {
        <span class="hljs-attr">rules</span>: [
            {
                <span class="hljs-attr">test</span>: <span class="hljs-regexp">/\.less$/</span>,
                <span class="hljs-attr">use</span>: [<span class="hljs-string">&apos;style-loader&apos;</span>, <span class="hljs-string">&apos;less-loader&apos;</span>]
            }
        ]
    },
    <span class="hljs-attr">plugins</span>: [
        <span class="hljs-keyword">new</span> EntryOptionWebpackPlugin(),
        <span class="hljs-keyword">new</span> AfterPlugins(),
        <span class="hljs-keyword">new</span> RunPlugin(),
        <span class="hljs-keyword">new</span> CompileWebpackPlugin(),
        <span class="hljs-keyword">new</span> AfterCompileWebpackPlugin(),
        <span class="hljs-keyword">new</span> EmitWebpackPlugin(),
        <span class="hljs-keyword">new</span> DoneWebpackPlugin()
    ]
}
</code></pre>
<h3 id="t246.2 Compiler">6.2 Compiler <a href="#t246.2 Compiler"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">const</span> path = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;path&apos;</span>);
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;fs&apos;</span>);
<span class="hljs-keyword">const</span> babylon = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;babylon&apos;</span>);
<span class="hljs-keyword">const</span> t = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;babel-types&apos;</span>);
<span class="hljs-keyword">const</span> generate = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;babel-generator&apos;</span>).default;
<span class="hljs-keyword">const</span> traverse = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;babel-traverse&apos;</span>).default;
<span class="hljs-keyword">const</span> ejs = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;ejs&apos;</span>);
<span class="hljs-keyword">const</span> { SyncHook } = <span class="hljs-built_in">require</span>(<span class="hljs-string">&apos;tapable&apos;</span>);

<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Compiler</span> </span>{
    <span class="hljs-keyword">constructor</span>(options) {
        <span class="hljs-keyword">this</span>.options = options;
        <span class="hljs-keyword">this</span>.hooks = {
            <span class="hljs-attr">entryOption</span>: <span class="hljs-keyword">new</span> SyncHook(),
            <span class="hljs-attr">afterPlugins</span>: <span class="hljs-keyword">new</span> SyncHook(),
            <span class="hljs-attr">run</span>: <span class="hljs-keyword">new</span> SyncHook(),
            <span class="hljs-attr">compile</span>: <span class="hljs-keyword">new</span> SyncHook(),
            <span class="hljs-attr">afterCompile</span>: <span class="hljs-keyword">new</span> SyncHook(),
            <span class="hljs-attr">emit</span>: <span class="hljs-keyword">new</span> SyncHook([<span class="hljs-string">&quot;compiler&quot;</span>]),
            <span class="hljs-attr">afterEmit</span>: <span class="hljs-keyword">new</span> SyncHook(),
            <span class="hljs-attr">done</span>: <span class="hljs-keyword">new</span> SyncHook()
        }
        <span class="hljs-keyword">let</span> plugins = options.plugins;
        <span class="hljs-keyword">if</span> (plugins &amp;&amp; plugins.length &gt; <span class="hljs-number">0</span>) {
            plugins.forEach(<span class="hljs-function"><span class="hljs-params">plugin</span> =&gt;</span> plugin.apply(<span class="hljs-keyword">this</span>));
        }
        <span class="hljs-keyword">this</span>.hooks.afterPlugins.call();
    }
    run() {
        <span class="hljs-keyword">this</span>.hooks.run.call(<span class="hljs-keyword">this</span>);
        <span class="hljs-keyword">let</span> that = <span class="hljs-keyword">this</span>;
        <span class="hljs-keyword">this</span>.root = process.cwd();<span class="hljs-comment">//&#x83B7;&#x53D6;&#x5F53;&#x524D;&#x7684;&#x5DE5;&#x4F5C;&#x76EE;&#x5F55;</span>
        <span class="hljs-keyword">let</span> { entry } = <span class="hljs-keyword">this</span>.options;<span class="hljs-comment">//&#x83B7;&#x53D6;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#x8DEF;&#x5F84;</span>
        <span class="hljs-keyword">this</span>.entryId = <span class="hljs-literal">null</span>;<span class="hljs-comment">//&#x8BB0;&#x5F55;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#x7684;ID</span>
        <span class="hljs-keyword">this</span>.modules = {};<span class="hljs-comment">//&#x8BB0;&#x5F55;&#x6A21;&#x5757;ID&#x548C;&#x5185;&#x5BB9;&#x7684;&#x5BF9;&#x5E94;&#x5173;&#x7CFB;&#xFF0C;&#x6240;&#x6709;&#x7684;ID&#x90FD;&#x662F;&#x76F8;&#x5BF9;&#x4E8E;&#x6839;&#x76EE;&#x5F55;&#x7684;</span>
        <span class="hljs-keyword">this</span>.hooks.compile.call();
        <span class="hljs-keyword">this</span>.buildModule(path.resolve(<span class="hljs-keyword">this</span>.root, entry), <span class="hljs-literal">true</span>);<span class="hljs-comment">//&#x4ECE;&#x5165;&#x53E3;&#x6587;&#x4EF6;&#x5F00;&#x59CB;&#x7F16;&#x8BD1;</span>
        <span class="hljs-keyword">this</span>.hooks.afterCompile.call();
        <span class="hljs-keyword">this</span>.emitFile();
    }
    emitFile() {
        <span class="hljs-keyword">this</span>.hooks.emit.call(<span class="hljs-keyword">this</span>, <span class="hljs-keyword">this</span>);
        <span class="hljs-keyword">let</span> mainTemplate = fs.readFileSync(path.join(__dirname, <span class="hljs-string">&apos;main.ejs&apos;</span>), <span class="hljs-string">&apos;utf8&apos;</span>);
        <span class="hljs-keyword">let</span> { modules, entryId } = <span class="hljs-keyword">this</span>;
        <span class="hljs-keyword">let</span> main = ejs.compile(mainTemplate)({ entryId, modules });
        <span class="hljs-keyword">let</span> { <span class="hljs-attr">output</span>: { <span class="hljs-attr">path</span>: dist, filename } } = <span class="hljs-keyword">this</span>.options;
        fs.writeFileSync(path.join(dist, filename), main);
        <span class="hljs-keyword">this</span>.hooks.afterEmit.call();
        <span class="hljs-keyword">this</span>.hooks.done.call();
    }
}
<span class="hljs-built_in">module</span>.exports = Compiler;
</code></pre>
<h2 id="t257.&#x652F;&#x6301;&#x61D2;&#x52A0;&#x8F7D;">7.&#x652F;&#x6301;&#x61D2;&#x52A0;&#x8F7D; <a href="#t257.&#x652F;&#x6301;&#x61D2;&#x52A0;&#x8F7D;"> # </a></h2>
<h3 id="t267.1 Compiler">7.1 Compiler <a href="#t267.1 Compiler"> # </a></h3>
<pre><code class="lang-js">emitFile() {
        <span class="hljs-keyword">this</span>.hooks.emit.call(<span class="hljs-keyword">this</span>, <span class="hljs-keyword">this</span>);
        <span class="hljs-keyword">let</span> mainTemplate = fs.readFileSync(path.join(__dirname, <span class="hljs-string">&apos;main.ejs&apos;</span>), <span class="hljs-string">&apos;utf8&apos;</span>);
        <span class="hljs-keyword">let</span> { modules, entryId } = <span class="hljs-keyword">this</span>;
        <span class="hljs-keyword">let</span> main = ejs.compile(mainTemplate)({ entryId, modules });
        <span class="hljs-keyword">let</span> { <span class="hljs-attr">output</span>: { <span class="hljs-attr">path</span>: dist, filename } } = <span class="hljs-keyword">this</span>.options;
        fs.writeFileSync(path.join(dist, filename), main);
        <span class="hljs-built_in">Object</span>.entries(<span class="hljs-keyword">this</span>.chunks).forEach(<span class="hljs-function">(<span class="hljs-params">[chunkIndex, chunk]</span>) =&gt;</span> {
            <span class="hljs-keyword">let</span> chunkTemplate = fs.readFileSync(path.join(__dirname, <span class="hljs-string">&apos;chunk.ejs&apos;</span>), <span class="hljs-string">&apos;utf8&apos;</span>);
            <span class="hljs-keyword">let</span> chunkData = ejs.compile(chunkTemplate)({ chunkIndex, chunk });
            <span class="hljs-keyword">let</span> { <span class="hljs-attr">output</span>: { <span class="hljs-attr">path</span>: dist, filename } } = <span class="hljs-keyword">this</span>.options;
            fs.writeFileSync(path.join(dist, <span class="hljs-string">`<span class="hljs-subst">${chunkIndex}</span>.bundle.js`</span>), chunkData);
        });
        <span class="hljs-keyword">this</span>.hooks.afterEmit.call();
        <span class="hljs-keyword">this</span>.hooks.done.call();
    }

    <span class="hljs-comment">//&#x89E3;&#x6790;&#x6A21;&#x5757;&#x548C;&#x4F9D;&#x8D56;&#x7684;&#x6A21;&#x5757;&#xFF0C;&#x8DEF;&#x5F84;&#x662F;&#x4E00;&#x4E2A;&#x7EDD;&#x5BF9;&#x8DEF;&#x5F84;</span>
    buildModule(modulePath, isEntry, chunkIndex) {
        <span class="hljs-keyword">let</span> source = <span class="hljs-keyword">this</span>.getSource(modulePath);<span class="hljs-comment">//&#x83B7;&#x53D6;&#x6E90;&#x4EE3;&#x7801;</span>
        <span class="hljs-keyword">let</span> moduleId = <span class="hljs-string">&apos;./&apos;</span> + path.relative(<span class="hljs-keyword">this</span>.root, modulePath);<span class="hljs-comment">//&#x751F;&#x6210;&#x76F8;&#x5BF9;&#x4E8E;&#x5DE5;&#x4F5C;&#x6839;&#x76EE;&#x5F55;&#x7684;&#x6A21;&#x5757;ID</span>
        <span class="hljs-keyword">if</span> (isEntry) {<span class="hljs-comment">//&#x5982;&#x679C;&#x662F;&#x5165;&#x53E3;&#x7684;&#x8BDD;&#x628A;ID&#x8D4B;&#x7ED9;&#x5165;&#x53E3;</span>
            <span class="hljs-keyword">this</span>.entryId = moduleId;
        }
        <span class="hljs-comment">//&#x83B7;&#x53D6;AST&#x7684;&#x7F16;&#x8BD1;&#x7ED3;&#x679C; &#x4F9D;&#x8D56;&#x7684;&#x6A21;&#x5757; &#x8F6C;&#x6362;&#x540E;&#x7684;&#x6E90;&#x4EE3;&#x7801;</span>
        <span class="hljs-keyword">let</span> { dependencies, sourcecode } = <span class="hljs-keyword">this</span>.parse(source, path.dirname(moduleId));
        <span class="hljs-keyword">if</span> (<span class="hljs-keyword">typeof</span> chunkIndex != <span class="hljs-string">&apos;undefined&apos;</span>) {
            <span class="hljs-keyword">let</span> currentChunk = <span class="hljs-keyword">typeof</span> <span class="hljs-keyword">this</span>.chunks[chunkIndex] == <span class="hljs-string">&apos;undefined&apos;</span> ? {} : <span class="hljs-keyword">this</span>.chunks[chunkIndex];
            currentChunk[moduleId] = sourcecode;
            <span class="hljs-keyword">this</span>.chunks[chunkIndex] = currentChunk;
        } <span class="hljs-keyword">else</span> {
            <span class="hljs-keyword">this</span>.modules[moduleId] = sourcecode;
        }

        <span class="hljs-comment">//&#x9012;&#x5F52;&#x89E3;&#x6790;&#x4F9D;&#x8D56;&#x7684;&#x6A21;&#x5757;</span>
        dependencies.forEach(<span class="hljs-function"><span class="hljs-params">dependency</span> =&gt;</span> <span class="hljs-keyword">this</span>.buildModule(path.join(<span class="hljs-keyword">this</span>.root, dependency, chunkIndex)));
    }
    <span class="hljs-comment">//&#x89E3;&#x6790;&#x6E90;&#x4EE3;&#x7801;  &#x4F20;&#x5165;&#x7236;&#x8DEF;&#x5F84;</span>
    parse(source, parentPath) {
        <span class="hljs-keyword">let</span> that = <span class="hljs-keyword">this</span>;
        <span class="hljs-keyword">const</span> ast = babylon.parse(source, {
            <span class="hljs-attr">plugins</span>: [<span class="hljs-string">&apos;dynamicImport&apos;</span>]
        });
        <span class="hljs-keyword">let</span> dependencies = [];
        traverse(ast, {
            CallExpression(p) {
                <span class="hljs-keyword">if</span> (p.node.callee.name == <span class="hljs-string">&apos;require&apos;</span>) {
                    <span class="hljs-keyword">let</span> node = p.node;
                    node.callee.name = <span class="hljs-string">&apos;__webpack_require__&apos;</span>;
                    <span class="hljs-keyword">let</span> modName = node.arguments[<span class="hljs-number">0</span>].value;
                    modName += (modName.lastIndexOf(<span class="hljs-string">&apos;.&apos;</span>) &gt; <span class="hljs-number">0</span> ? <span class="hljs-string">&apos;&apos;</span> : <span class="hljs-string">&apos;.js&apos;</span>);
                    <span class="hljs-keyword">let</span> moduleId = <span class="hljs-string">&apos;./&apos;</span> + path.join(parentPath, modName);
                    dependencies.push(moduleId);
                    node.arguments = [t.stringLiteral(moduleId)];
                } <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (t.isImport(p.node.callee)) {
                    <span class="hljs-keyword">let</span> node = p.node;
                    <span class="hljs-keyword">let</span> modName = node.arguments[<span class="hljs-number">0</span>].value;<span class="hljs-comment">//&#x53D6;&#x5F97;&#x6A21;&#x5757;&#x540D;</span>
                    modName += (modName.lastIndexOf(<span class="hljs-string">&apos;.&apos;</span>) &gt; <span class="hljs-number">0</span> ? <span class="hljs-string">&apos;&apos;</span> : <span class="hljs-string">&apos;.js&apos;</span>);
                    <span class="hljs-keyword">let</span> moduleId = <span class="hljs-string">&apos;./&apos;</span> + path.join(parentPath, modName);
                    p.replaceWithSourceString(<span class="hljs-string">`__webpack_require__.e(<span class="hljs-subst">${that.chunkIndex}</span>).then(__webpack_require__.t.bind(null, &quot;<span class="hljs-subst">${moduleId}</span>&quot;, 7))`</span>);
                    that.buildModule(path.join(that.root, moduleId), <span class="hljs-literal">false</span>, that.chunkIndex++);
                }
            }
        });
        <span class="hljs-keyword">let</span> sourcecode = generate(ast).code;
        <span class="hljs-keyword">return</span> { sourcecode, dependencies };
    }
</code></pre>
<h3 id="t277.2 chunk.ejs">7.2 chunk.ejs <a href="#t277.2 chunk.ejs"> # </a></h3>
<pre><code class="lang-js">(<span class="hljs-built_in">window</span>[<span class="hljs-string">&quot;webpackJsonp&quot;</span>] = <span class="hljs-built_in">window</span>[<span class="hljs-string">&quot;webpackJsonp&quot;</span>] || []).push([[<span class="xml"><span class="hljs-tag">&lt;<span class="hljs-name">%=chunkIndex%</span>&gt;</span>],{
    <span class="hljs-tag">&lt;<span class="hljs-name">%</span>
        <span class="hljs-attr">for</span>(<span class="hljs-attr">let</span> <span class="hljs-attr">id</span> <span class="hljs-attr">in</span> <span class="hljs-attr">chunk</span>){
            <span class="hljs-attr">let</span> <span class="hljs-attr">source</span> = <span class="hljs-string">chunk[id];%</span>&gt;</span>
        /***/ &quot;<span class="hljs-tag">&lt;<span class="hljs-name">%-id%</span>&gt;</span>&quot;:
        /***/ function(module, exports,__webpack_require__) {

        eval(`<span class="hljs-tag">&lt;<span class="hljs-name">%-source%</span>&gt;</span>`);

        /***/ },
        <span class="hljs-tag">&lt;<span class="hljs-name">%}%</span>&gt;</span>
    }]);
</span></code></pre>
<h3 id="t287.3 index.js">7.3 index.js <a href="#t287.3 index.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-keyword">let</span> loadButton = <span class="hljs-built_in">document</span>.querySelector(<span class="hljs-string">&apos;#loadButton&apos;</span>);
loadButton.addEventListener(<span class="hljs-string">&apos;click&apos;</span>, () =&gt; {
    <span class="hljs-keyword">import</span>(<span class="hljs-string">&apos;./video&apos;</span>).then(<span class="hljs-function"><span class="hljs-params">video</span> =&gt;</span> {
        <span class="hljs-built_in">console</span>.log(video.default);
    });
});
</code></pre>
<h3 id="t297.4 video.js">7.4 video.js <a href="#t297.4 video.js"> # </a></h3>
<pre><code class="lang-js"><span class="hljs-built_in">module</span>.exports = <span class="hljs-string">&apos;video&apos;</span>;
</code></pre>
<h2 id="t30&#x53C2;&#x8003;&#x8D44;&#x6599;">&#x53C2;&#x8003;&#x8D44;&#x6599; <a href="#t30&#x53C2;&#x8003;&#x8D44;&#x6599;"> # </a></h2>
<ul>
<li><a href="https://gitee.com/zhufengpeixun/webpackhand">webpackhand</a></li>
<li><a href="https://astexplorer.net/">astexplorer</a></li>
<li><a href="https://github.com/jamiebuilds/babel-handbook/blob/master/translations/en/plugin-handbook.md">plugin-handbook</a></li>
<li><a href="https://github.com/jamiebuilds/babel-handbook/blob/master/translations/en/plugin-handbook.md#toc-babel-types">babel-types</a></li>
<li><a href="https://www.npmjs.com/package/babel-types">babel-types</a></li>
</ul>

        <div class="copyright">Powered by <a href="https://github.com/jaywcjlove/idoc" target="_blank">idoc</a>. Dependence <a href="https://nodejs.org">Node.js</a> run.</div>
    </div>
    
</div>

<script src="https://cdn.bootcss.com/jquery/3.0.0/jquery.js"></script>
<script>
$('.warpper .page-toc ul ul li a').on('click',function(){
  $('.warpper .page-toc ul ul li a').removeClass('my-active')
  $(this).addClass('my-active')
})
  // if (!$('.understand-me').length) {
  //   var bar = $(window).height() - $('.navbar ').height() - $('.page-toc').position().top;
  //   var count = bar / 26 / 2;
  //   var barHeight = $('.page-toc').outerHeight();
  //   $('.page-toc li').eq(0).children('a').addClass('red');
  //   var arr = [];
  //   $("h1,h2,h3,h4,h5,h6").each(function () {
  //     arr.push($(this).position().top);
  //   });
  //   var timer
  //   function dark() {
  //     clearTimeout(timer)
  //      timer = setTimeout(function () {
  //      var top = Math.abs($('.page-toc > ul').position().top);
  //      var cur = $('.content').scrollTop();
  //      for (var i = arr.length; i >= 0; i--) {
  //        if (arr[i] <= cur) {
  //          break;
  //        }
  //      }
  //      if (i === -1) {
  //        i = 0;
  //      }
  //      $('.page-toc li a').removeClass('red');
  //      $('.page-toc li').eq(i).children('a').addClass('red');
  //      let height = $('.page-toc li').eq(i).position().top-$('.page-toc').height(); // 如果当前的offset出去了 回到中间可好？
  //      $('.page-toc').scrollTop(height+$('.page-toc').height()/2);
  //    },200)
  //   }

  //   $('.content').on('scroll', dark);
  // }
</script>
<style>

    /* ::-webkit-scrollbar{width:14px;}
    ::-webkit-scrollbar-track{background-color:transparent;}
    ::-webkit-scrollbar-thumb{background-color:transparent;}
    ::-webkit-scrollbar-thumb:hover {background-color:transparent}
    ::-webkit-scrollbar-thumb:active {background-color:transparent} */

    .page-toc > ul .red {
        background: #f3f3f3;
        z-index: 1;
        border-left: 3px solid #009a61;
        -webkit-transition: all .2s ease;
        transition: all .2s ease;
        color: #000
    }





</style>
</body>
</html>
